/**
 The MIT License

Copyright (c) <2011> <JPACHUBE Team>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */
package com.pachube.commons.impl;

import com.pachube.api.Data;

/**
 * The Class DataImpl.
 */
public class DataImpl implements Data {

    /** The id. */
    private int id;

    /** The tag. */
    private String tag;

    /** The value. */
    private double value;

    /** The min value. */
    private Double minValue;

    /** The max value. */
    private Double maxValue;

    /**
     * Instantiates a new data impl.
     * 
     * @param id the id
     * @param tag the tag
     * @param value the value
     * @param minValue the min value
     * @param maxValue the max value
     */
    public DataImpl(int id, String tag, double value, Double minValue,
            Double maxValue) {
        this.id = id;
        this.tag = tag;
        this.value = value;
        this.minValue = minValue;
        this.maxValue = maxValue;
    }

    /**
     * Instantiates a new data impl.
     */
    public DataImpl() {

    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#getId()
     */
    public int getId() {
        return this.id;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#setId(java.lang.String)
     */
    public void setId(String id) {
        this.id = Integer.parseInt(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#setId(int)
     */
    public void setId(int id) {
        this.id = id;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#getTag()
     */
    public String getTag() {
        return this.tag;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#setTag(java.lang.String)
     */
    public void setTag(String tag) {
        this.tag = tag;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#getValue()
     */
    public double getValue() {
        return this.value;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#setValue(java.lang.String)
     */
    public void setValue(String value) {
        this.value = Double.parseDouble(value);
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#setValue(double)
     */
    public void setValue(double value) {
        this.value = value;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#getMinValue()
     */
    public Double getMinValue() {
        return this.minValue;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#setMinValue(java.lang.Double)
     */
    public void setMinValue(Double minValue) {
        if (minValue != null) {
            this.minValue = minValue;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#setMinValue(java.lang.String)
     */
    public void setMinValue(String minValue) {
        if (minValue != null) {
            this.minValue = Double.parseDouble(minValue);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#getMaxValue()
     */
    public Double getMaxValue() {
        return this.maxValue;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#setMaxValue(java.lang.Double)
     */
    public void setMaxValue(Double maxValue) {
        if (maxValue != null) {
            this.maxValue = maxValue;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#setMaxValue(java.lang.String)
     */
    public void setMaxValue(String maxValue) {
        if (this.minValue != null) {
            this.maxValue = Double.parseDouble(maxValue);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#toXMLWithWrapper()
     */
    public String toXMLWithWrapper() {
        String ret = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<eeml xmlns=\"http://www.eeml.org/xsd/005\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"5\" xsi:schemaLocation=\"http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd\"><environment>";
        ret = ret + this.toXML() + "</environment></eeml>";
        return ret;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#toXML()
     */
    public String toXML() {
        String ret = "";
        ret = "<data id=\"" + this.id + "\">\n\t\t";
        ret = ret + "<tag>" + this.tag + "</tag>\n\t\t";
        ret = ret + "<value ";

        if (this.minValue != null) {
            ret = ret + "minValue=\"" + this.minValue + "\" ";
        }

        if (this.maxValue != null) {
            ret = ret + "maxValue=\"" + this.maxValue + "\" ";
        }
        ret = ret + ">" + this.value + "</value>\n\t";

        ret = ret + "</data>";

        return ret;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Pachube.Data#toString()
     */
    @Override
    public String toString() {
        return "Data [id=" + this.id + ", maxValue=" + this.maxValue
                + ", minValue=" + this.minValue + ", tag=" + this.tag
                + ", value=" + this.value + "]";
    }

}
